<?php 

namespace app\manager\controller;
use think\Db;
/**
 * 订单管理
 */
class Order extends \think\Controller
{
	use \app\common\traits\controller\TraitController;
	const ORDER_OBLIGATION_STATUS = 100;//待付款
    const ORDER_STAYIN_STATUS = 101;//待入住
    const ORDER_REFUND_ON = 102;//退款中
    const ORDER_CANCEL_STATUS = 103;//取消订单
    const ORDER_ONGOING_STATUS = 104;//支付中
    const ORDER_CHECKIN_STATUS = 200;//入住中
    const ORDER_LEAVE_STATUS = 201;//已离开 
    const ORDER_REFUND_DEPOSIT_STATUS = 202;//返还押金中
    const ORDER_REFUND_FINISH = 203;//已完成的订单
    
	protected function _initialize(){
		if (!model('CampManagers')->checkLogin()) {
            $this->redirect('@Campmanager/login');
            exit;
        }
		$this->model = model('Orders');
		$this->list_route = url('/orders');
	}
	/**
	 * 待付款订单信息
	 */
	public function getIndexPay(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_OBLIGATION_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_w_p = count($order);
	    return view('order/index_pay',['order'=>$order,'order_w_p'=>$order_w_p]);
	}

	/**
	 * 付款中订单信息
	 */
	public function getIndexOngoingPay(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_ONGOING_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_o_p = count($order);
	    return view('order/index_ongoingpay',['order'=>$order,'order_o_p'=>$order_o_p]);
	}
	
	/**
	 * 待入住订单信息
	 */
	public function getIndexWaitIn(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_STAYIN_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_d_z = count($order);
	    return view('order/index_wait_in',['order'=>$order,'order_d_z'=>$order_d_z]);
	}
	
	/**
	 * 退款中的订单信息.........................................
	 */
	public function getRefundOn(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_REFUND_ON,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_t_k = count($order);
	    return view('order/refund_on',['order'=>$order,'order_t_k'=>$order_t_k]);
	}
	
	/**
	 * 入住中订单信息
	 */
	public function getIndexIn(){
	    $query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_CHECKIN_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_r_z = count($order);
	    return view('order/index_in',['order'=>$order,'order_r_z'=>$order_r_z]);
	}
	
	/**
	 * 已离开的订单信息.......................................
	 */
	public function getLeaved(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_LEAVE_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_y_k = count($order);
	    return view('order/leaved',['order'=>$order,'order_y_k'=>$order_y_k]);
	}
	
	/**
	 * 返还押金中的订单信息.......................................
	 */
	public function getRefundDeposit(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_REFUND_DEPOSIT_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_f_y = count($order);
	    return view('order/refund_deposit',['order'=>$order,'order_f_y'=>$order_f_y]);
	}
	
	/**
	 * 已完成订单信息
	 */
	public function getIndexComplete(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_REFUND_FINISH,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_w_c = count($order);
	    return view('order/index_complete',['order'=>$order,'order_w_c'=>$order_w_c]);
	}
	
	/**
	 * 取消的订单信息
	 */
	public function getIndexCancel(){
		$query = input('get.');
		$order = $this->model->getOrders(array_merge($query,['status' => self::ORDER_CANCEL_STATUS,'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]));
	    $order_q_x = count($order);
	    return view('order/index_cancel',['order'=>$order,'order_q_x'=>$order_q_x]);
	}
	
	/**
	 * 订单详情
	 */
	public function getRead(){
		$order = $this->model->getOrderInfo(input('get.id')); 
		$leave_car_date=strtotime($order['leave_car_date']);
        $take_car_date=strtotime($order['take_car_date']);
        $date=($leave_car_date-$take_car_date)/86400;
	    return view('order/read',['order' => $order,'date'=>$date]);
	} 
	 
	  /**
	  * 已离开的订单详情
	  */
	public function getLeavedRead(){
		$order = $this->model->getOrderInfo(input('get.id')); 
		$leave_car_date=strtotime($order['leave_car_date']);
        $take_car_date=strtotime($order['take_car_date']);
        $date=($leave_car_date-$take_car_date)/86400;
		$deduct = $this->model->deduct();
	    return view('order/leaved_read',['order'=>$order,'date'=>$date,'deduct'=>$deduct]);
	} 

	/**
	 * 退还押金................................................营地需要的
	 */
	public function postQuitDeposit(){
		//查看订单是已离开的
		$data = model('Orders')->where(['number'=>input('post.num'),'status'=>self::ORDER_LEAVE_STATUS])->find();
		if(!$data){
			$this->error('该订单不存在');
			return;
		}
		//实际退的押金
		$deposit = $data->deposit-input('post.deduct/d');
	    if(model('QuitDeposits')->addInfo(['number' => $data->number,'user_id' => $data->rent_user_id,'deposit' => $deposit,'status'=>self::ORDER_REFUND_DEPOSIT_STATUS,'describe'=>input('post.describe')])){
	    	if($this->model->where('number',$data->number)->update(['status'=>self::ORDER_REFUND_DEPOSIT_STATUS])){
	    		$this->success('申请成功','/orders/refunddeposit');
	    	}
	    }
		$this->error('申请失败');
	}
	 
	/**
	 * 入住用户身份表单
	 */
	public function getIndex(){
	    return view('order/index');
	} 
	 
	  
	/**
	 * 获取手机验证码
	 */ 
	public function postCode(){
		if(!model('Users')->where('mobile',input('post.mobile'))->find()){
			return json(['status'=>0,'message'=>'注册账户手机号不存在']);
		}
		if(model('PhoneCode')->Send(input('post.mobile'))){
			return json(['status'=>0,'message'=>'手机验证码已发送，请注意查收']);
		}
	} 
	 
	/**
	 * 入住身份验证
	 */ 
	public function postIndex(){
		if(model('PhoneCode')->Check(input('post.code'),session_id()) !== true){
			return $this->error(Model('PhoneCode')->getError());
		}
		if(!model('Users')->where('mobile',input('post.mobile'))->find()){
			return $this->error('注册账户手机号不存在');
		}
		
		return $this->success('验证通过','/orders/list?mobile='.input('post.mobile'));
	}
	 
	/**
	 * 入住用户订单列表
	 */ 
	public function getList(){
		$userData = model('Users')->where('mobile',input('get.mobile'))->find();
		$order = $this->model->getOrders(['status' => self::ORDER_STAYIN_STATUS,'rent_user_id'=>$userData['id'],'camp_id'=>model('CampManagers')->getLoginUserInfo('camp_id')]);
		return view('order/index_list',['order'=>$order,'userData'=>$userData]);
	} 
	 
	/**
	 * 改变-->待入住状态为-->入住中状态
	 */
	public function getChangeStayInOrder(){
		$where['id'] = input('get.id/d');
		$where['number'] = input('get.num');
		$where['status'] = self::ORDER_STAYIN_STATUS;
		if(model('Orders')->where($where)->update(['status'=>self::ORDER_CHECKIN_STATUS])){
			$this->success('修改成功','/orders/indexin');
		}
		$this->error('修改失败');
	}
	 
	 /**
	 * 改变-->入住中状态为-->已离开状态
	 */
	public function getChangeLeavedOrder(){
		$where['id'] = input('get.id/d');
		$where['number'] = input('get.num');
		$where['status'] = self::ORDER_CHECKIN_STATUS;
		if(model('Orders')->where($where)->update(['status'=>self::ORDER_LEAVE_STATUS])){
			$this->success('修改成功','/orders/leaved');
		}
		$this->error('修改失败');
	}
	 
}






 ?>